/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiruss.imageprocessor.ui;

import com.hiruss.imageprocessor.util.Filters;
import com.hiruss.imageprocessor.util.ImageHandler;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;

/**
 *
 * @author Hiru
 */
public class FilterPanel extends javax.swing.JDialog {

    private UI parent;
    private static FilterPanel instance;

    /**
     * Creates new form CropPanel
     */
    private FilterPanel(UI parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        setLocationRelativeTo(parent);
    }

    public static FilterPanel getInstance(UI parent) {
        if (FilterPanel.instance == null) {
            FilterPanel.instance = new FilterPanel(parent, true);
        }
        instance.revertButton.setEnabled(false);
        return FilterPanel.instance;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        resamplingMethodButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        applyButton = new javax.swing.JButton();
        revertButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        threasholdAvgTextField = new javax.swing.JTextField();
        alphaTrimmedPTextField = new javax.swing.JTextField();
        mean3x3FilterRadioButton = new javax.swing.JRadioButton();
        median3x3FilterRadioButton = new javax.swing.JRadioButton();
        mean5x5FilterRadioButton = new javax.swing.JRadioButton();
        median5x5FilterRadioButton = new javax.swing.JRadioButton();
        maxFilterRadioButton = new javax.swing.JRadioButton();
        threasholdAvgFilterRadioButton = new javax.swing.JRadioButton();
        alphaTrimmedFilterRadioButton = new javax.swing.JRadioButton();
        minFilterRadioButton = new javax.swing.JRadioButton();
        midPointFilterRadioButton = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Resample");
        setBounds(new java.awt.Rectangle(0, 0, 400, 120));
        setMinimumSize(new java.awt.Dimension(620, 180));
        setModal(true);
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridLayout(1, 3, 6, 0));

        applyButton.setText("Apply");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });
        jPanel1.add(applyButton);

        revertButton.setText("Revert");
        revertButton.setEnabled(false);
        revertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                revertButtonActionPerformed(evt);
            }
        });
        jPanel1.add(revertButton);

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        jPanel1.add(closeButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(6, 12, 12, 12);
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        threasholdAvgTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        threasholdAvgTextField.setText("50");
        threasholdAvgTextField.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 6);
        jPanel2.add(threasholdAvgTextField, gridBagConstraints);

        alphaTrimmedPTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        alphaTrimmedPTextField.setText("2");
        alphaTrimmedPTextField.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 6);
        jPanel2.add(alphaTrimmedPTextField, gridBagConstraints);

        resamplingMethodButtonGroup.add(mean3x3FilterRadioButton);
        mean3x3FilterRadioButton.setSelected(true);
        mean3x3FilterRadioButton.setText("Mean Filter 3x3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 3, 3);
        jPanel2.add(mean3x3FilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(median3x3FilterRadioButton);
        median3x3FilterRadioButton.setText("Median Filter 3x3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(median3x3FilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(mean5x5FilterRadioButton);
        mean5x5FilterRadioButton.setText("Mean Filter 5x5");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 3, 3);
        jPanel2.add(mean5x5FilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(median5x5FilterRadioButton);
        median5x5FilterRadioButton.setText("Weighted Median Filter 5x5");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(median5x5FilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(maxFilterRadioButton);
        maxFilterRadioButton.setText("Max Filter");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(maxFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(threasholdAvgFilterRadioButton);
        threasholdAvgFilterRadioButton.setText("Threashold Avarage");
        threasholdAvgFilterRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                threasholdAvgFilterRadioButtonStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(threasholdAvgFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(alphaTrimmedFilterRadioButton);
        alphaTrimmedFilterRadioButton.setText("Alpha Trimmed Filter P=");
        alphaTrimmedFilterRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                alphaTrimmedFilterRadioButtonStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(alphaTrimmedFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(minFilterRadioButton);
        minFilterRadioButton.setText("Min Filter");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(minFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(midPointFilterRadioButton);
        midPointFilterRadioButton.setText("Mid Point Filter");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 3, 3);
        jPanel2.add(midPointFilterRadioButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 6, 12);
        getContentPane().add(jPanel2, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
        
        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        applyButton.setEnabled(false);
        try {
            BufferedImage originalImage = parent.getEditingImage();
            BufferedImage destImage = parent.getEditingImage();

            if (mean3x3FilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyFilter(originalImage, Filters.MEAN_FILTER_3X3, Filters.TYPE.AVARAGE, -1);
            } else if (mean5x5FilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyFilter(originalImage, Filters.MEAN_FILTER_5X5, Filters.TYPE.AVARAGE, -1);
            } else if (median3x3FilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyFilter(originalImage, Filters.MEAN_FILTER_3X3, Filters.TYPE.MEDIAN, -1);
            } else if (median5x5FilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyFilter(originalImage, Filters.MEDIAN_FILTER_HORIZONTAL, Filters.TYPE.MEDIAN, -1);
            } else if (midPointFilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyFilter(originalImage, Filters.MEAN_FILTER, Filters.TYPE.MID, -1);
            } else if (maxFilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyFilter(originalImage, Filters.MEAN_FILTER, Filters.TYPE.MAX, -1);
            } else if (minFilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyFilter(originalImage, Filters.MEAN_FILTER, Filters.TYPE.MIN, -1);
            } else if (threasholdAvgFilterRadioButton.isSelected()) {
                int threashold = Integer.parseInt(threasholdAvgTextField.getText());
                destImage = ImageHandler.applyFilter(
                        originalImage, Filters.MEAN_FILTER, Filters.TYPE.AVARAGE_THREASHOLD, threashold);
            } else if (alphaTrimmedFilterRadioButton.isSelected()) {
                int p = Integer.parseInt(alphaTrimmedPTextField.getText());
                destImage = ImageHandler.applyFilter(
                        originalImage, Filters.MEAN_FILTER, Filters.TYPE.ALPHA_TRIMMED, p);
            }
            parent.setEditingImage(destImage);
            revertButton.setEnabled(parent.isRevertible());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,
                    e.getLocalizedMessage(),
                    "Invalid Inputs!",
                    JOptionPane.WARNING_MESSAGE);
        }
        applyButton.setEnabled(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_applyButtonActionPerformed

    private void revertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_revertButtonActionPerformed

        parent.revert();
//        parent.getEditingImageFrame().setFrameSize(tempEditingImage.getWidth(), tempEditingImage.getHeight());
        revertButton.setEnabled(false);
    }//GEN-LAST:event_revertButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed

        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private void threasholdAvgFilterRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_threasholdAvgFilterRadioButtonStateChanged

        threasholdAvgTextField.setEnabled(threasholdAvgFilterRadioButton.isSelected());
    }//GEN-LAST:event_threasholdAvgFilterRadioButtonStateChanged

    private void alphaTrimmedFilterRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_alphaTrimmedFilterRadioButtonStateChanged

        alphaTrimmedPTextField.setEnabled(alphaTrimmedFilterRadioButton.isSelected());
    }//GEN-LAST:event_alphaTrimmedFilterRadioButtonStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton alphaTrimmedFilterRadioButton;
    private javax.swing.JTextField alphaTrimmedPTextField;
    private javax.swing.JButton applyButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton maxFilterRadioButton;
    private javax.swing.JRadioButton mean3x3FilterRadioButton;
    private javax.swing.JRadioButton mean5x5FilterRadioButton;
    private javax.swing.JRadioButton median3x3FilterRadioButton;
    private javax.swing.JRadioButton median5x5FilterRadioButton;
    private javax.swing.JRadioButton midPointFilterRadioButton;
    private javax.swing.JRadioButton minFilterRadioButton;
    private javax.swing.ButtonGroup resamplingMethodButtonGroup;
    private javax.swing.JButton revertButton;
    private javax.swing.JRadioButton threasholdAvgFilterRadioButton;
    private javax.swing.JTextField threasholdAvgTextField;
    // End of variables declaration//GEN-END:variables
}
